import 'package:library_base/entities/stock_out_order_detail_child_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/inventory_distribution/base_inventory_distribution_single_controller.dart';
import 'package:get/get.dart';

class InventoryDistributionSingleController extends BaseInventoryDistributionSingleController {

  late StockOutOrderDetailChildEntity stockOutOrderDetailChildEntity;

  late Function(StockOutOrderDetailChildEntity stockOutOrderDetailChildEntity, int position) updateUi;

  late int position;

  @override
  void onCreate() {
    super.onCreate();
    stockOutOrderDetailChildEntity = getArguments()['temporaryData'];
    updateUi = getArguments()['callback'];
    position = getArguments()['index'];
  }



  @override
  void distribute(){
    if(checkedStockOutOrderGoodsDetailEntity.isEmpty){
      ToastUtil.normal(MultiLanguage.pleaseSelectItemsInStock.tr);
      return;
    }
    var map = {};
    map['stockOutId'] = stockOutId;
    map['goodsCode'] = goodsCode;

    var goodsDetails = [];
    double actualOutCount = 0.0;
    double actualOutWeight = 0.0;
    for(int i = 0;i<checkedStockOutOrderGoodsDetailEntity.length;i++){
      var selectData = checkedStockOutOrderGoodsDetailEntity[i];
      var detail = {};
      detail['goodsBarCode'] = selectData.goodsBarCode;
      detail['batchNo'] = selectData.batchNo;
      detail['locationId'] = selectData.locationId;
      detail['stockOutCount'] = 1.0;
      detail['stockOutWeight'] = selectData.usableWeight;
      goodsDetails.add(detail);
      actualOutCount = NumUtil.add(actualOutCount, selectData.stockOutCount??0.0);
      actualOutWeight = NumUtil.add(actualOutWeight, selectData.usableWeight);
    }
    map['goodsDetails'] = goodsDetails;
    requestUniversal<bool>(Apis.STOCK_OUT_LOCK_GOODS,
        tipSuccess: MultiLanguage.successfulDistributionGoods.tr,
        isShowToastTip: true,
        params: map, resultSuccessCall: (result) async {
          stockOutOrderDetailChildEntity.goodsDetails = checkedStockOutOrderGoodsDetailEntity;
          stockOutOrderDetailChildEntity.actualOutCount = actualOutCount;
          stockOutOrderDetailChildEntity.actualOutWeight = actualOutWeight;
          updateUi(stockOutOrderDetailChildEntity,position);
          Get.back();
        });
  }

}
